﻿@{
    ViewBag.Title = "Add";
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
}




@section Header{

    <script type="text/javascript">
         
        $(document).ready(function () {
            window.UEDITOR_HOME_URL = "/UploadFile/";
        });
         

        function submit() {
            //编辑器对象
            var uEditor = UE.getEditor('txt_News');

            //验证输入
            if ($("#txt_Title").validatebox('isValid') == false) { return; }

            if (uEditor.hasContents() == false) {
                $.messager.alert('失败', '请输入内容', 'error');
                return;
            }

            //获取附件列表
            var attachmentString = "";
            //---------注意：这个地方有点恶心，冒号分割文件名和保存的的文件名，竖号分割每个附件，在实体类里面进行组装list
            $div = $('#AttarchmentDIV').children('div');
            $div.each(
                function () {
                    attachmentString += $(this).attr('fileName') + ':' + $(this).attr('saveName') + '|';
                }
            );


            //参数对象
            var news = {
                NewsGroupName: $('#cbxGroupName').combobox('getValue'),
                IsTop: document.getElementById("cbx_IsTop").checked,
                TiTle: $("#txt_Title").val(),
                NewsContent: uEditor.getContent(),
                AttarchmentString: attachmentString
            };

            $.post("/News/AddNews/", news,
                    function (data, textStatus) {
                        if (data == 'success') {
                            $("#txt_Title").val(""); //清空
                            uEditor.setContent(""); //清空
                            $('#AttarchmentDIV').empty();

                            $.messager.alert('成功', '添加成功', 'info');
                        }
                        else { $.messager.alert('失败', data, 'error'); }
                    }
            );
        }

        function removeAttarchment(divID) {
            $('#' + divID).remove();
        }

        //附件上传成功
        function onUploadSuccess(file, data, response) {
            eval("data=" + data);
            var fileId = data.FileID;
            $('#AttarchmentDIV').append('<div id="' + fileId + '" fileName = "' + file.name + '" saveName = "' + data.SaveName + '">' + file.name + '<a href = "#" onclick = "removeAttarchment(\'' + fileId + '\')">删除</a></div>');
        }
    </script>
}

<body>

    <div>
        标题:
    @Html.TextBox("txt_Title", "", new { size = 50, @class = "easyui-validatebox", required = "true" })
        <br />
        新闻分组：
    <select id="cbxGroupName" class="easyui-combobox" name="state" style="width: 200px;">
        @{ 
            foreach (string item in ViewBag.NewsGroup)
            { 
            <option value="@item">@item</option>  
            }
        }
    </select>
        <br />

        是否置顶：  @Html.CheckBox("cbx_IsTop", false)
        <br />
        @Html.UEditor("txt_News")
        <br />
        @Html.Uploader("file_uploader", "onUploadSuccess")
        <div id="AttarchmentDIV"></div>

        <p>
            <input type="button" value="添加" onclick="submit()" />
        </p>
    </div>
</body>
